package cn.rongdao.jrkabao.ui.message.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.message.LoanMessageBean;


/**
 * Created by jiangyongxing on 2016/7/21.
 * 描述：
 */
public class LoanMessageRecyclerViewAdapter extends RecyclerView.Adapter<LoanMessageRecyclerViewAdapter.AllLoanFragmentRecyclerViewHolder> {

    private Context mContext;
    private List<LoanMessageBean> list;
    private OnItemActionListener mOnItemActionListener;

    public LoanMessageRecyclerViewAdapter(Context context, List<LoanMessageBean> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public AllLoanFragmentRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_mymessage, parent, false);
        AllLoanFragmentRecyclerViewHolder holder = new AllLoanFragmentRecyclerViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final AllLoanFragmentRecyclerViewHolder holder, int position) {
        holder.itemName.setText(list.get(position).getTitle());
        holder.itemTime.setText(list.get(position).getTime());
        holder.itemInfo.setText(String.valueOf(list.get(position).getData()));

        if (mOnItemActionListener != null) {
            holder.itemRl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //注意这里必须使用viewHolder.getPosition()而不能用i，因为为了保证动画，没有使用NotifyDatasetChanged更新位置数据
                    mOnItemActionListener.onItemClickListener(v, holder.getPosition());
                }
            });
            holder.itemRl.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    //注意这里必须使用viewHolder.getPosition()而不能用i，因为为了保证动画，没有使用NotifyDatasetChanged更新位置数据
                    return mOnItemActionListener.onItemLongClickListener(v, holder.getPosition());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void setOnItemActionListener(OnItemActionListener onItemActionListener) {
        this.mOnItemActionListener = onItemActionListener;
    }

    /**********
     * 定义点击事件
     **********/
    public interface OnItemActionListener {
        public void onItemClickListener(View v, int pos);

        public boolean onItemLongClickListener(View v, int pos);
    }

    class AllLoanFragmentRecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView itemName;
        public TextView itemTime;
        public RelativeLayout itemRl;
        public TextView itemInfo;

        public AllLoanFragmentRecyclerViewHolder(View layout) {
            super(layout);
            itemName = (TextView) layout.findViewById(R.id.item_mymessage_name);
            itemTime = (TextView) layout.findViewById(R.id.item_mymessage_time);
            itemInfo = (TextView) layout.findViewById(R.id.item_mymessage_info);
            itemRl = (RelativeLayout) layout.findViewById(R.id.item_mymessage_rl);
        }
    }

}
